package com.pj.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.pj.fragment.adapter.ListItemAdapter;
import com.pj.fragment.bean.WebListBean;
import com.pj.fragment.fragment.WebFragment;
import com.pj.fragment.util.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private ListView titleList;
    private FrameLayout fragment;
    private boolean isPad = false;
    private List<WebListBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        titleList.setAdapter(new ListItemAdapter(data));
        titleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isPad){
                    //如果为平板，则更新右侧的fragment，将网址的url用构造方法传输过去
                    replaceFragment(new WebFragment(data.get(i).getUrl()));
                }else {
                    //如果是手机，则将url用Intent传参带到WebActivity
                    startActivity(new Intent(MainActivity.this,WebActivity.class).putExtra("url",data.get(i).getUrl()));
                }
            }
        });
    }

    private void initView() {
        isPad = isPad(MainActivity.this);
        titleList = (ListView) findViewById(R.id.title_list);
        if (isPad) {
            fragment = (FrameLayout) findViewById(R.id.fragment);
        }
        data = new ArrayList<>();
        data.add(new WebListBean("苹果","https://www.apple.com.cn",R.drawable.apple));
        data.add(new WebListBean("华为","https://www.huawei.com/cn/",R.drawable.huawei));
        data.add(new WebListBean("小米","https://m.mi.com/",R.drawable.mi));
        data.add(new WebListBean("oppo","https://www.oppo.com/cn/",R.drawable.oppo));
        data.add(new WebListBean("vivo","http://www.vivo.com.cn/",R.drawable.vivo));
    }

    //用FragmentManager更换Fragment显示
    public void replaceFragment(Fragment fragment){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment,fragment);

//        transaction.addToBackStack(null);
        transaction.commit();
    }

    //用于判断是否为平板
    public static boolean isPad(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

}